package com.loc.dao;

import java.util.Collection;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Component;

import com.loc.pojo.Menu;
import com.loc.security.MySecurityMetadataSource;

@Component
public class MenusDao {

	@Autowired
	private BaseDao baseDao;
	
	@SuppressWarnings("unchecked")
	public List<Menu> findAll() throws Exception {
		String sql = "select * from loc_menu_resource lmr order by lmr.menu_id asc";
		List<Menu> menu = baseDao.query(sql, Menu.mapRow(Menu.class));
		return menu;
	}

	public List<Map<String, Object>> findMenuByUser(UserDetails userDetails)
			throws Exception {
		String sql = "";
		Collection<GrantedAuthority> collection = userDetails.getAuthorities();
		boolean flag = false;
		for(GrantedAuthority ga:collection){
			if(ga.getAuthority().equals("ROLE_ADMIN")){
				flag = true;
				break;
			}
		}
		if (flag) {
			sql = "select * from loc_menu_resource lmr where isvisiable=1 order by lmr.menu_id asc";
		} else {
			sql = "select * from loc_user lu "
					+ "inner join loc_user_role lur on lu.user_id=lur.user_id "
					+ "inner join loc_role lr on lur.role_id=lr.role_id "
					+ "inner join loc_role_resource lrr on lrr.role_id = lr.role_id "
					+ "inner join loc_menu_resource lmr on lmr.menu_id = lrr.menu_id "
					+ "where lu.user_account='"
					+ userDetails.getUsername()
					+ "' and lmr.isvisiable=1 order by lmr.menu_id asc";
		}
		List<Map<String, Object>> list = baseDao.queryForList(sql);
		return list;
	}

	public List<Map<String, Object>> findAllMenu(UserDetails userDetails) throws Exception {
		String sql = "";
		Collection<GrantedAuthority> collection = userDetails.getAuthorities();
		boolean flag = false;
		for(GrantedAuthority ga:collection){
			if(ga.getAuthority().equals("ROLE_ADMIN")){
				flag = true;
				break;
			}
		}
		if (flag) {
			sql = "select * from loc_menu_resource lmr order by lmr.menu_id asc";
		} else {
			sql = "select * from loc_menu_resource lmr where issys<>-1 order by lmr.menu_id asc";
		}
		List<Map<String, Object>> list = baseDao.queryForList(sql);
		return list;
	}

	public List<Map<String, Object>> findMenuByMenuId(UserDetails userDetails)
			throws Exception {
		String sql = "";
		Collection<GrantedAuthority> collection = userDetails.getAuthorities();
		boolean flag = false;
		for(GrantedAuthority ga:collection){
			if(ga.getAuthority().equals("ROLE_ADMIN")){
				flag = true;
				break;
			}
		}
		if (flag) {
			sql = "select * from loc_menu_resource lmr order by lmr.menu_id asc";
		} else {
			sql = "select * from loc_menu_resource lmr where issys<>-1 order by lmr.menu_id asc";
		}
		List<Map<String, Object>> list = baseDao.queryForList(sql);
		return list;
	}

	public void addMenu(Menu menu) throws Exception {
		String sql = "insert into loc_menu_resource (menu_name,menu_url,menu_parent,menu_icon,isleaf,isvisiable,issys,memo,menu_rel,menu_type) values(?,?,?,?,?,?,?,?,?,?)";
		baseDao.update(sql, menu.getMenu_name(), menu.getMenu_url(), menu
				.getMenu_parent(), menu.getMenu_icon(), menu.getIsleaf(), menu
				.getIsvisiable(), menu.getIssys(), menu.getMemo(), menu
				.getMenu_rel(), menu.getMenu_type());
		MySecurityMetadataSource.reload();
	}
	public Map<String, Object> selectMenu(String menu_id)throws Exception{
		String sql = "select * from loc_menu_resource lmr where lmr.menu_id=?";
		return baseDao.queryForMap(sql, Integer.valueOf(menu_id));
	}
	public void updateMenu(Menu menu)throws Exception {
		String sql = "update loc_menu_resource set menu_name=?,menu_url=?,menu_parent=?,menu_icon=?,isleaf=?,isvisiable=?,issys=?,memo=?,menu_rel=?,menu_type=? where menu_id=?";
		baseDao.update(sql, menu.getMenu_name(), menu.getMenu_url(), menu
				.getMenu_parent(), menu.getMenu_icon(), menu.getIsleaf(), menu
				.getIsvisiable(), menu.getIssys(), menu.getMemo(), menu
				.getMenu_rel(), menu.getMenu_type(),menu.getMenu_id());
		MySecurityMetadataSource.reload();
	}
	public void deleteMenu(String id)throws Exception{
		String sql = "delete from loc_menu_resource where menu_id="+id+" or menu_parent="+id;
		baseDao.update(sql);
		MySecurityMetadataSource.reload();
	}
	@SuppressWarnings("unchecked")
	public List<Menu> getMenuByIds(String ids)throws Exception{
		String sql = "select * from loc_menu_resource where menu_id in("+ids+")";
		return baseDao.query(sql, Menu.mapRow(Menu.class));
	}

	public List<Map<String, Object>> findMenus(Map<String,Object> map,UserDetails userDetails, int startRow, int rowsCount) {
		String sql = "";
		Collection<GrantedAuthority> collection = userDetails.getAuthorities();
		boolean flag = false;
		for(GrantedAuthority ga:collection){
			if(ga.getAuthority().equals("ROLE_ADMIN")){
				flag = true;
				break;
			}
		}
		if (flag) {
			sql = "select * from loc_menu_resource lmr where 1=1";
			if(map.containsKey("menu_name")){
				sql+=" and lmr.menu_name like '%"+map.get("menu_name")+"%'";
			}
			if(map.containsKey("menu_type")){
				sql+=" and lmr.menu_type="+map.get("menu_type");
			}
			if(map.containsKey("isleaf")){
				sql+=" and lmr.isleaf="+map.get("isleaf");
			}
			if(map.containsKey("isvisiable")){
				sql+=" and lmr.isvisiable="+map.get("isvisiable");
			}
			if(map.containsKey("menu_rel")){
				sql+=" and lmr.menu_rel="+map.get("menu_name");
			}
			if(map.containsKey("menu_parent")){
				sql+=" and lmr.menu_parent="+map.get("menu_parent");
			}
			sql+=" order by lmr.menu_id asc";
		} else {
			sql = "select * from loc_menu_resource lmr where issys<>-1";
			if(map.containsKey("menu_name")){
				sql+=" and lmr.menu_name like '%"+map.get("menu_name")+"%'";
			}
			if(map.containsKey("menu_type")){
				sql+=" and lmr.menu_type="+map.get("menu_type");
			}
			if(map.containsKey("isleaf")){
				sql+=" and lmr.isleaf="+map.get("isleaf");
			}
			if(map.containsKey("isvisiable")){
				sql+=" and lmr.isvisiable="+map.get("isvisiable");
			}
			if(map.containsKey("menu_rel")){
				sql+=" and lmr.menu_rel="+map.get("menu_name");
			}
			if(map.containsKey("menu_parent")){
				sql+=" and lmr.menu_parent="+map.get("menu_parent");
			}
			sql+=" order by lmr.menu_id asc";
		}
		List<Map<String, Object>> list = baseDao.queryForListPage(sql, startRow, rowsCount);
		return list;
	}
}
